package com.kkb.kkbcommon.basic;

import java.util.Random;

/**
 * @Author: zml
 * @Description:
 * @Data Created in 2021-08-05 20:47
 * @Modified by:
 */
public class RandomUtil {
    /**
     * @Author: zml
     * @Description: 根据传入的范围参数获取一个随机数，范围为[0, range)
     * @Data Created in 2021-08-02 10:43:26
     * @Modified by:
     *
     * @param range 范围参数
     * @return 获取到的随机数
     */
    public static int getRandomInt(int range){
        return new Random().nextInt(range);
    }

    /**
     * @Author: zml
     * @Description: 根据范围获取随机数[start, end)
     * @Data Created in 2021-08-02 10:43:45
     * @Modified by:
     *
     * @param start 选择的起点
     * @param end 选择的终点
     * @return 获取到的随机数
     */
    public static int getRandomInt(int start, int end){
        return getRandomInt(end-start) + start;
    }

    /**
     * @Author: zml
     * @Description: 获取随机的小写单词
     * @Data Created in 2021-08-02 10:44:05
     * @Modified by:
     *
     * @return 获取到的小写单词
     */
    public static char getRandomLowercaseChar(){return (char) getRandomInt(97, 123);}

    /**
     * @Author: zml
     * @Description: 获取随机的大写单词
     * @Data Created in 2021-08-02 10:44:23
     * @Modified by:
     *
     * @return 获取到的大写单词
     */
    public static char getRandomCapitalizedChar(){return (char) getRandomInt(65, 91);}

    /**
     * @Author: zml
     * @Description: 获取随机的数字
     * @Data Created in 2021-08-02 10:44:38
     * @Modified by:
     *
     * @return 获取到的随机数
     */
    public static char getRandomDigitChar(){return (char) getRandomInt(48, 58);}

    /**
     * @Author: zml
     * @Description: 获取一个16进制的随机数
     * @Data Created in 2021-08-02 10:44:55
     * @Modified by:
     *
     * @return 获取到的16进制随机数
     */
    public static char getRandomHexadecimalChar(){
        return Integer.toHexString(getRandomInt(16)).toUpperCase().charAt(0);
    }

    /**
     * @Author: zml
     * @Description: 获取一个随机的中文字符
     * @Data Created in 2021-08-02 10:45:10
     * @Modified by:
     *
     * @return 获取到的随机中文字符
     */
    public static char getRandomChineseChar(){
        return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1)));
    }

    /**
     * @Author: zml
     * @Description: 根据格式获取随机数
     *              w 小写英文
     *              W 大写英文
     *              d 数字
     *              x 16进制数
     *              c 中文
     * @Data Created in 2021-08-02 10:45:28
     * @Modified by:
     *
     * @param format 格式化参数
     * @return 生成的随机数
     */
    public static String getRandomCode(String format){
        StringBuilder result = new StringBuilder();
        for (char c : format.toCharArray()) {
            switch (c){
                case 'w':
                    result.append(getRandomLowercaseChar());
                    break;
                case 'W':
                    result.append(getRandomCapitalizedChar());
                    break;
                case 'd':
                    result.append(getRandomDigitChar());
                    break;
                case 'x':
                    result.append(getRandomHexadecimalChar());
                    break;
                case 'c':
                    result.append(getRandomChineseChar());
                    break;
                default:
                    result.append(c);
                    break;
            }
        }
        return result.toString();
    }
}
